<?php
header("content-type:text/html;charset=utf8");
$dbh=new PDO('mysql:host=localhost;dbname=mhsw','root','root');
$sql="select * from news";
$arr=$dbh->query($sql)->fetchAll(PDO::FETCH_ASSOC);
?>
<table border="1">
    <tr>
        <td>编号</td>
        <td>新闻名称</td>
        <td>新闻内容</td>
        <td>操作</td>
    </tr>
    <?php foreach($arr as $key=>$value) { ?>
        <tr>
            <td><?php echo $value['id']?></td>
            <td><?php echo $value['news_name']?></td>
            <td><?php echo $value['news_content']?></td>
            <td>
                <a href="javascript:void(0)" onclick="del(<?php echo $value['id']?>,this)">删除</a>
                <a href="update.php?id=<?php echo $value['id']?>">修改</a>
            </td>
        </tr>
    <?php } ?>
</table>
 <script>
    function del(id,obj)
    {
        var ajax=new XMLHttpRequest();//先实例化
        ajax.onreadystatechange=function()
        {
            if(ajax.readyState==4&&ajax.status==200)
            {
                if(ajax.responseText==1)
                {
                    obj.parentNode.parentNode.remove();
                }
                else
                {
                    alert("删除失败");
                }
            }
        }
        ajax.open('get','uu.php?id='+id,true);
        ajax.send();
    }

</script>
